package com.api.workflow.web;

import com.api.workflow.util.YNHttpUtils;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.MainTableInfo;
import weaver.soa.workflow.request.Property;
import weaver.soa.workflow.request.RequestInfo;

import java.util.HashMap;
import java.util.Map;

/*
 * MMI003采购退回
 * */
public class YNMMI003Action extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        BaseBean baseBean = new BaseBean();
        String requestid = requestInfo.getRequestid();
        int formid = requestInfo.getRequestManager().getFormid();
        int formidAbs = Math.abs(formid);
        String tableName = "formtable_main_" + formidAbs;
        // 主表数据
        MainTableInfo mainTableInfo = requestInfo.getMainTableInfo();
        Property[] property = mainTableInfo.getProperty();
        Map<String, Object> mainTableInfoMap = new HashMap<>();
        for (int i = 0; i < property.length; i++) {
            String name = property[i].getName().toLowerCase();
            mainTableInfoMap.put(name, Util.null2String(property[i].getValue()));
        }
        baseBean.writeLog("MMI001=======mainTableInfoMap:" + com.alibaba.fastjson.JSONObject.toJSONString(mainTableInfoMap));

        try {
            JSONArray jsonArray1 = new JSONArray();
            //主表数据
            JSONObject result = new JSONObject();
            result.put("INSTID", mainTableInfoMap.get("lcbh"));
            result.put("BANFN", mainTableInfoMap.get("cgsqbh"));
            result.put("BNFPO", mainTableInfoMap.get("cgsqxxm"));
            result.put("ZTHYY", mainTableInfoMap.get("thyy"));

            jsonArray1.add(result);
            baseBean.writeLog("wyq===MMI003采购退回===jsonArray1" + jsonArray1.toString());
            String post = YNHttpUtils.MMI003(jsonArray1.toString(), YNHttpUtils.token());
            baseBean.writeLog("wyq===MMI003采购退回===post" + post);
            JSONArray jsonArray = JSONObject.parseArray(post);

            RecordSet recordSet = new RecordSet();
            //qqcs
            String updateMainTableName = "update " + tableName + " set   qqcsx = ? , fhjgx = ?  where  requestid = ? ";
            recordSet.executeUpdate(updateMainTableName, jsonArray1.toString(), post, requestid);

            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                String STATUS = jsonObject.getString("returnstatus");
                String MESSAGE = jsonObject.getString("returnmsg");

                if (!"S".equals(STATUS)) {
                    requestInfo.getRequestManager()
                            .setMessageid(requestInfo.getRequestid() + "MMI005");
                    requestInfo.getRequestManager().setMessagecontent("SAP接口异常，请联系SAP人员，错误信息提示::" + MESSAGE);
                    return Action.FAILURE_AND_CONTINUE;
                } else {
                    // 推送完成 修改原始数据的状态 2 已退回
                    String update = "update  uf_cgsqd  set  dqzt = 2   where  cgsqbh = ? and cgsqxxm = ? ";
                    RecordSet recordSet1 = new RecordSet();
                    boolean b = recordSet1.executeUpdate(update, mainTableInfoMap.get("cgsqbh"), mainTableInfoMap.get("cgsqxxm"));
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
            requestInfo.getRequestManager()
                    .setMessageid(requestInfo.getRequestid() + "MMI005");
            requestInfo.getRequestManager().setMessagecontent("接口异常:" + e.toString());
            return Action.FAILURE_AND_CONTINUE;
        }
        return Action.SUCCESS;

    }

}